<template>
  <div class="container">
    <div class="content_wraper">
      <div>
        <div class="left_cate">
          <p class="title">植物列表</p>
          <div class="left_con">
            <Tree
              ref="tree"
              :tree-data="treeData"
              @treeNodeClick="treeNodeClick"
            >
            </Tree>
          </div>
        </div>
        <div class="right_box">
          <TopSearch @search="handleSearch" />
          <div class="result_box">
            <div class="result_contain">
              <div
                class="result_item"
                @click="goDetail(item.id)"
                v-for="(item, index) in plantList"
                :key="index"
              >
                <el-row :gutter="10" style="height: 100%">
                  <el-col :xs="24" :sm="24" :lg="8" style="height: 100%">
                    <div style="background: #f1f2f3; height: 100%">
                      <img
                        :src="imgUrl + item.img"
                        style="
                          width: 100%;
                          height: 100%;
                          object-fit: fill;
                          border-radius: 3px;
                        "
                      />
                    </div>
                  </el-col>
                  <el-col :xs="24" :sm="24" :lg="16" style="height: 100%">
                    <div
                      style="
                        display: flex;
                        flex-direction: column;
                        padding: 5px 0;
                        height: 100%;
                      "
                    >
                      <div style="line-height: 36px">
                        植物学名：{{ item.scientificName }}
                      </div>
                      <el-descriptions title="" :column="1" style="flex: 1">
                        <el-descriptions-item label="科属">{{
                          item.lauraceae
                        }}</el-descriptions-item>
                        <el-descriptions-item label="分类"
                          ><dict-tag
                            :options="dict.type.plant_type"
                            :value="item.plantType"
                        /></el-descriptions-item>
                        <el-descriptions-item label="习性">{{
                          item.habit
                        }}</el-descriptions-item>
                      </el-descriptions>
                    </div>
                  </el-col>
                </el-row>
              </div>
            </div>
          </div>
          <Pagination :total="total" :limit="5" @pagination="pageChange" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Tree from "@/components/Tree";
import Pagination from "@/components/Pagination";
import TopSearch from "./components/TopSearch.vue";

import { getPlantList, getPlantTree } from "@/api/plant.js";

export default {
  name: "PlantMaterial",
  components: {
    Tree,
    TopSearch,
    Pagination
  },
  dicts: ["plant_type"],
  data() {
    return {
      total: 0,
      query: {
        current: 1,
        size: 5
      },
      plantList: [],
      plantTypeList: []
    };
  },
  computed: {
    imgUrl() {
      return process.env.VUE_APP_IP || "";
    },
    treeData() {
      return {
        showTree: true,
        isSearchShow: false,
        title: {
          isShow: false,
          setting: false,
          text: ""
        },
        defaultProps: {
          children: "children",
          label: "nodeName"
        },
        data: this.plantTypeList
      };
    }
  },
  created() {
    this.getPlantList();
    this.getTreeselect();
  },
  methods: {
    handleSearch(params) {
      console.log(params);
      this.query = Object.assign(this.query, params);
      this.getPlantList();
    },
    async getTreeselect() {
      const res = await getPlantTree();
      const { data } = res;
      this.plantTypeList = data;
    },
    async getPlantList() {
      const res = await getPlantList(this.query);
      const { data } = res;
      this.plantList = data.records;
      this.total = data.total;
    },
    goDetail(id) {
      this.$router.push({ path: "/pdetail", query: { plantId: id } });
    },
    treeNodeClick(obj, node) {
      if (node.level === 2) {
        const id = obj.bizId;
        this.$router.push({ path: "/pdetail", query: { plantId: id } });
      }
    },
    pageChange(obj) {
      this.query.current = obj.page;
      this.getPlantList();
    }
  }
};
</script>

<style lang="scss" scoped>
.result_item {
  padding: 5px;
  margin-bottom: 15px;
  height: 25vh;
  // border: 1px solid #ccc;
  box-shadow: 0 2px 14px 0 rgba(0, 0, 0, 0.1);
  cursor: pointer;
  &:hover {
    background: #fff;
    box-shadow: 0 2px 14px 0 rgba(0, 0, 0, 0.3);
    border-radius: 4px;
    z-index: 1;
  }
}
@media screen and (max-width: 1130px) {
  .content_wraper .left_cate {
    margin-right: 24px;
  }
  .content_wraper .right_box {
    width: calc(100% - 256px);
  }
}
@media screen and (max-width: 909px) {
  .content_wraper .left_cate {
    display: none;
  }
  .content_wraper .right_box {
    width: 100%;
  }
}

.container {
  min-height: calc(100vh - 300px);
  background-color: #fff;
  // background-image: url("../../assets/imgs/p_bg.jpg");
  // background-size: cover;
  // background-repeat: no-repeat;
}
.content_wraper {
  height: 100%;
  max-width: 1130px;
  // background: #fff;
  margin: auto;
  padding: 24px 0;
  // font-size: 0;
}
.left_cate {
  background: #fff;
  width: 235px;
  margin: 0 38px 0 0;
  display: inline-block;
  vertical-align: top;
  border-radius: 4px;
  min-height: 80vh;
  padding: 0 5px;
  // box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.1);
  .title {
    display: flex;
    justify-content: center;
    margin: 16px auto 0px;
    font-size: 16px;
    color: #333;
    line-height: 22px;
    font-weight: 700;
  }
  .left_con {
    min-height: 77vh;
    padding: 12px;
    border-radius: 5px;
    // box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.1);
    .type_item {
      font-size: 0;
      margin-bottom: 16px;
      overflow: hidden;
      position: relative;
      span {
        font-size: 14px;
        color: #999;
        margin: 5px 9px 0;
        line-height: 28px;
        display: inline-block;
        vertical-align: middle;
        cursor: pointer;
      }
    }
  }
}
.right_box {
  border-radius: 4px;
  display: inline-block;
  vertical-align: top;
  width: 852px;
  padding: 0 0 85px;
  background: #fff;

  min-height: 654px;
  position: relative;
  // padding-bottom: 85px;
}

.result_box {
  padding-top: 16px;
  // height: 700px;
  // background-color: #f1f2f3;
}
</style>
